﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;

public partial class Pregled : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Baza baza = new Baza();
        string sqlString = "SELECT naslov,opis,datum,lokacija " +
                           "FROM nastan " +
                           "WHERE nastan_id=" + Request.QueryString["id"].ToString(); 
        
        DataSet ds = baza.Zemi(sqlString, "Nastan");

        sqlString="SELECT id_slika "+
                  "FROM slika "+
                  "WHERE nastan_id=" + Request.QueryString["id"].ToString();
        DataSet ds1 = baza.Zemi(sqlString, "slika");

        lblNaslov.Text = ds.Tables["Nastan"].Rows[0][0].ToString();
        lblOpis.Text = FormatirajOpis(ds.Tables["Nastan"].Rows[0][1].ToString()); ;
        string text = ds.Tables["Nastan"].Rows[0][2].ToString();
        DateTime datum = Convert.ToDateTime(text);
        lblDatum.Text = datum.ToShortDateString();
        lblLokacija.Text = ds.Tables["Nastan"].Rows[0][3].ToString();

        imgSlika.ImageUrl = "Handler.ashx?id=" + ds1.Tables["slika"].Rows[0][0].ToString() ;
        GetImageInfo(Request.QueryString["id"].ToString());

        PlaceHolder1.Controls.Add(new LiteralControl("<br /><br />"));
        if (ds1.Tables["slika"].Rows.Count > 1)
        {
            for (int i = 1; i < ds1.Tables["slika"].Rows.Count; i++)
            {
                Image img = new Image();
                img.Width = 400;
                img.Height = 300;
                PlaceHolder1.Controls.Add(img);
                PlaceHolder1.Controls.Add(new LiteralControl("<br /><br />"));
                img.ImageUrl = "Handler.ashx?id=" + ds1.Tables["slika"].Rows[i][0].ToString();
                GetImageInfo(Request.QueryString["id"].ToString()); 
            }
        }

        postaviVideo();
        zemiKomentari();
    }

    private void zemiKomentari()
    {
        phKomentari.Controls.Clear();
        phKomentari.Controls.Add(new LiteralControl("<br />"));
        Baza baza = new Baza();
        string sqlString = "SELECT ime,text,grad"
                    + " FROM komentar "
                    + " WHERE nastan_id=" + Request.QueryString["id"].ToString()
                    + " ORDER BY komentar_id DESC";

        DataSet ds2 = baza.Zemi(sqlString, "komentar");
        int k;
        if (ds2.Tables["komentar"] == null) k = 0;
        else k = ds2.Tables["komentar"].Rows.Count;
        lblVkupno.Text = k.ToString();

        for (int i = 0; i < k; i++)
        {
            Label nov = new Label();
            nov.Width = 500;
            nov.BackColor = System.Drawing.Color.DarkGray;
            nov.Font.Bold = true;
            phKomentari.Controls.Add(nov);

            phKomentari.Controls.Add(new LiteralControl("<br />"));

            Label comment = new Label();
            comment.Width = 500;
            comment.BackColor = System.Drawing.Color.LightGray;
            phKomentari.Controls.Add(comment);

            phKomentari.Controls.Add(new LiteralControl("<br /><br />"));

            nov.Text = ds2.Tables["komentar"].Rows[i][0].ToString() + " од " + ds2.Tables["komentar"].Rows[i][2].ToString();
            comment.Text = ds2.Tables["komentar"].Rows[i][1].ToString();
        }
    }

    private string FormatirajOpis(String opis)
    {
        string pom = opis;
        int k = 0;
        List<int> lista = new List<int>();
        int ii = pom.LastIndexOf(".");
        int j = 0;
        while (pom.LastIndexOf('.') != pom.IndexOf('.'))
        {
            int i = pom.IndexOf('.');
            if (j == 3)
            {
                string pom1 = opis.Substring(0, k + i + 1);
                pom1 += "<br /> <br/>";
                string pom2 = opis.Substring(k + i + 1);
                pom1 += opis.Substring(k + i + 1);
                opis = pom1;
                j = 0;
                k += "<br /> <br/>".Length;
            }
            else j++;
            k = k + i + 1;
            pom = pom.Substring(i + 1);
        }
        return opis;
    }

    private void GetImageInfo(string id)
    {
        Baza baza = new Baza();      
        string sqlString = "SELECT picture FROM slika WHERE nastan_id ="+id;
        DataSet ds = baza.Zemi(sqlString, "Slika"); 
         
    }
    protected void btnVnesi_Click(object sender, EventArgs e)
    {
        string conn = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        SqlConnection konekcija = new SqlConnection(conn);
        string sqlString = "INSERT INTO komentar " +
                    "VALUES(@ime, @komentar, @grad, @nastan)";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        komanda.Parameters.AddWithValue("@ime", txtImeKomentar.Text);
        komanda.Parameters.AddWithValue("@grad", txtGrad.Text);
        komanda.Parameters.AddWithValue("@komentar", txtKomentar.Text);
        komanda.Parameters.AddWithValue("@nastan", Request.QueryString["id"].ToString());

        try
        {
            konekcija.Open();
            komanda.ExecuteNonQuery();
            konekcija.Close();
        }
        catch (Exception err)
        {
            Label1.Text = err.Message;
        }
        zemiKomentari();
    }
    protected void postaviVideo()
    {
        Baza baza = new Baza();
        string sqlString = "SELECT id_video " +
                  "FROM video " +
                  "WHERE nastan_id=" + Request.QueryString["id"].ToString();
        DataSet ds1 = baza.Zemi(sqlString, "video");
        if (ds1.Tables["video"].Rows.Count > 0)
        {
            fplVideo.Visible = true;
            fplVideo.VideoFile = "HandlerVideo.ashx?id=" + ds1.Tables["video"].Rows[0][0].ToString();
            lblVideo.Visible = true;
        }
    }
}